package com.atguigu.spzx.manager.controller;

import com.atguigu.spzx.manager.service.SysUserService;
import com.atguigu.spzx.model.common.Result;
import com.atguigu.spzx.model.dto.sys.LoginDto;
import com.atguigu.spzx.model.entity.sys.SysUser;
import com.atguigu.spzx.model.vo.sys.LoginVo;
import com.atguigu.spzx.model.vo.sys.ValidateCodeVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.enums.ParameterIn;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.web.bind.annotation.*;

@RestController
// 前端因为携带了Cookie访问 所有后端必须配置
//originPatterns表示允许跨域访问的服务器域名/ip的表达式   * 表示所有
//allowedHeaders: * 表示允许携带任意请求头跨域访问  allowCredentials：true表示允许携带cookie跨域访问
//@CrossOrigin(originPatterns="*",allowedHeaders="*" , allowCredentials = "true" )
@RequestMapping("/admin/system/index")
@Tag(name = "Index模块")
public class IndexController {
    @Resource
    SysUserService sysUserService;
    //查询一定会返回数据
    //增删改 如果前端需要局部刷新可以返回增删改的数据
    //注销
    @Operation(summary = "注销接口" , description = "注销接口")
    @Parameters(value = {
            @Parameter(name = "token" ,description="登录令牌" ,
                    in = ParameterIn.HEADER,required = true)
    })
    @GetMapping("/logout")
    public Result logout(@RequestHeader("token")String token){
       sysUserService.logout(token);
        return Result.ok();
    }
    //查询用户信息回显： 使用SysUser响应数据
    // 前端会在请求头中携带token
    @Operation(summary = "获取用户信息接口" ,description = "获取用户信息接口")
    @GetMapping("/userInfo")
    public Result getUserInfo(){
        SysUser sysUser = sysUserService.getUserInfo();
        return Result.ok().data(sysUser);
    }
    @Operation(summary = "登录接口" ,description = "登录接口")
    @PostMapping("/login")
    public Result login(@RequestBody LoginDto loginDto){
        LoginVo loginVo = sysUserService.login(loginDto);
        return Result.ok().data(loginVo);
    }
    @Operation(summary = "获取验证码接口" ,description = "获取验证码接口")
    @GetMapping("/validateCodeImg")
    public Result validateCodeImg(){
        ValidateCodeVo validateCodeVo = sysUserService.genValidateCodeImg();
        return Result.ok().data(validateCodeVo);
    }


}
